%{
#include "ast.h"
#include "parser.tab.hpp"
#include <cstdlib>
#include <string>
#include <cstring> // 添加 strdup
#include <unistd.h>
%}

%option noyywrap nounput batch noinput nodefault

%%

[ \t\n\r]+    ; // 忽略空白
"/*"          { /* 跳过多行注释 */ }
"//".*        ; // 跳过单行注释

"int"         { return INT; }
"void"        { return VOID; }
"if"          { return IF; }
"else"        { return ELSE; }
"while"       { return WHILE; }
"break"       { return BREAK; }
"continue"    { return CONTINUE; }
"return"      { return RETURN; }

"("           { return LPAREN; }
")"           { return RPAREN; }
"{"           { return LBRACE; }
"}"           { return RBRACE; }
";"           { return SEMI; }
","           { return COMMA; }
"="           { return ASSIGN; }
"=="          { return EQ; }
"!="          { return NEQ; }
"<"           { return LT; }
"<="          { return LE; }
">"           { return GT; }
">="          { return GE; }
"&&"          { return AND; }
"||"          { return OR; }
"!"           { return NOT; }
"+"           { return PLUS; }
"-"           { return MINUS; }
"*"           { return MUL; }
"/"           { return DIV; }
"%"           { return MOD; }

[_a-zA-Z][_a-zA-Z0-9]* { yylval.str = strdup(yytext); return ID; }
[0-9]+        { yylval.num = atoi(yytext); return NUMBER; }

.             { fprintf(stderr, "非法字符: %c\n", *yytext); }

%%

